package com.poizon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.poizon.bo.CodeBo;
import com.poizon.bo.PasswordLoginBo;
import com.poizon.config.SystemConfig;
import com.poizon.dao.UserDao;
import com.poizon.dao.UserLogDao;
import com.poizon.dao.UserinfoDao;
import com.poizon.entity.User;
import com.poizon.entity.UserLog;
import com.poizon.entity.Userinfo;
import com.poizon.enumerate.UserOperaType;
import com.poizon.manager.impl.SmsServiceImpl;
import com.poizon.service.intf.UserService;
import com.poizon.utils.CheckUtils;
import com.poizon.utils.EncryptUtil;
import com.poizon.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName UserServiceImpl
 * @Author NoCheng
 * @Date 2022/1/2 11:30
 * @Description:TODO 用户登录业务实现层
 */

@Service
public class UserServiceImpl implements UserService {
    private final UserDao dao;
    private final UserinfoDao infoDao;
    private final UserLogDao logDao;
    private final SmsServiceImpl smsService;

    public UserServiceImpl(UserDao dao, UserinfoDao infoDao, UserLogDao logDao, SmsServiceImpl smsService) {
        this.dao = dao;
        this.infoDao = infoDao;
        this.logDao = logDao;
        this.smsService = smsService;
    }

    @Override
    public R login(PasswordLoginBo bo, String ip) {
        return null;
    }

    @Transactional
    @Override
    public R login(CodeBo bo, String ip) {
        //校验格式
        if (CheckUtils.isPhone(bo.getPhone()) && CheckUtils.isCode(bo.getCode() + "")){
            //判断是否存在
            User user = selectByPhone(bo.getPhone());
            if (user != null){
                //登录
                if (smsService.checkLoginCode(bo)){
                    //记录日志
                    logDao.insert(new UserLog(user.getId(), ip, "验证码登录", UserOperaType.LOGIN.getValue()));
                    return R.ok();
                }
            }else{
                //注册
                if (smsService.checkLoginCode(bo)){
                    User regUser = new User();
                    //手机号加密并赋值
                    regUser.setPhone(EncryptUtil.aesenc(SystemConfig.PASS_KEY, bo.getPhone()));
                    dao.insert(regUser);
                    infoDao.insert(new Userinfo(regUser.getId(), "默认头像", "随机昵称", new Date()));
                    //记录日志
                    logDao.insert(new UserLog(regUser.getId(), ip, "完成注册并登录", UserOperaType.REGISTER.getValue()));
                    return R.ok();
                }
            }
        }
        return R.fail("手机号或验证码错误");
    }


    @Override
    public User selectByPhone(String phone) {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("phone", EncryptUtil.aesenc(SystemConfig.PASS_KEY, phone));
        return dao.selectOne(query);
    }


    @Override
    public R loginOut(String token) {
        return null;
    }

    @Override
    public R checkToken(String token) {
        return null;
    }
}
